package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.CompanyDAO;
import entity.Company;
import entity.WebUser;

@WebServlet("/company")
public class CompanyServlet extends HttpServlet{
	private CompanyDAO dao=new CompanyDAO();
	private String action;
	 public CompanyServlet() {
	        super();
	    }

	 public void doGet(HttpServletRequest request, HttpServletResponse response)
	            throws ServletException, IOException {
	        doPost(request,response);
	    }
	 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			request.setCharacterEncoding("UTF-8");
			this.action = request.getParameter("action");
			if (action.equals("edit")) {
				edit(request);
			} else if(action.equals("del")) {
				del(request);
			}
			request.getRequestDispatcher("/companyList.jsp").forward(request, response);
		}

	 private String edit(HttpServletRequest request) {        
	        try {
	        	Company company = new Company();
		        company.setComno(request.getParameter("comno"));
		        company.setCcompany(request.getParameter("ccompany"));
		        company.setAddress(request.getParameter("address"));
		        company.setPhone(request.getParameter("phone"));
	            this.dao.Company_submit(company.getComno(),company.getCcompany(),company.getAddress(),company.getPhone());
	            return "/companyList.jsp";
	        } catch (Exception ex) {
	            request.setAttribute("errormsg", ex.getMessage());
	            return "/error.jsp";
	        }
	    }
	   private void del(HttpServletRequest request){
	   	 try {

	   		 this.dao.delCompany(request.getParameter("comno"));
			 } catch (Exception ex) {
				 
		         request.setAttribute("errormsg", ex.getMessage());
		         
		     }
	   }
}
